package feevale.dropbox.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import feevale.dropbox.service.Share;

@SuppressWarnings("serial")
public class ClientView extends JFrame {
	
	private static final String TITLE_FRM = "Dropbox - Sistemas Distribuídos";
	
	private static final String DEFAULT_HOST = "localhost";
	private static final Integer DEFAULT_PORT = 8899;
	private static final String DEFAULT_DIRECTORY = "C:\\Temp\\client";
	
	private JTextField txtHost;
	private JTextField txtPort;
	private JTextField txtDirectory;
	private JButton btnStart;
	private JButton btnExit;
	
	private Share share;
	
	public ClientView() {
		setTitle(TITLE_FRM);
		setSize(400, 200);
		setLayout(null);
		
		initLayout();
		setDefaultValues();
		
		setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	
	/**
	 * 
	 */
	private void initLayout() {
		JLabel lblHost = new JLabel("Host:");
		lblHost.setBounds(20, 30, 80, 20);
		add(lblHost);
		
		txtHost = new JTextField();
		txtHost.setBounds(100, 30, 150, 20);
		add(txtHost);
		
		JLabel lblPort = new JLabel("Porta:");
		lblPort.setBounds(260, 30, 50, 20);
		add(lblPort);
		
		txtPort = new JTextField();
		txtPort.setBounds(320, 30, 50, 20);
		txtPort.setEditable(false);
		add(txtPort);
		
		JLabel lblDirectory = new JLabel("Diretório:");
		lblDirectory.setBounds(20, 70, 80, 20);
		add(lblDirectory);
		
		txtDirectory = new JTextField();
		txtDirectory.setBounds(100, 70, 270, 20);
		add(txtDirectory);
		
		btnStart = new JButton("Iniciar");
		btnStart.setBounds(20, 110, 100, 25);
		add(btnStart);
		
		btnStart.addActionListener(getStartAction());
		
//		btnExit = new JButton("Sair");
//		btnExit.setBounds(270, 110, 100, 25);
//		btnExit.setEnabled(false);
//		add(btnExit);
//		
//		btnExit.addActionListener(getExitAction());
	}
	
	/**
	 * Atribui os valores default para os campos da tela
	 */
	private void setDefaultValues() {
		txtHost.setText(DEFAULT_HOST);
		txtPort.setText(DEFAULT_PORT.toString());
		txtDirectory.setText(DEFAULT_DIRECTORY);
	}
	
	private ActionListener getStartAction() {
		return new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				btnStart.setEnabled(false);
//				btnExit.setEnabled(true);
				
				try {
					Socket socket = new Socket(txtHost.getText(), Integer.parseInt(txtPort.getText()));
					
					share = new Share(socket, txtDirectory.getText());
					
				} catch (NumberFormatException | IOException e1) {
					e1.printStackTrace();
				}
			}
		};
	}
	
//	/**
//	 * 
//	 * @return
//	 */
//	private ActionListener getExitAction() {
//		return new ActionListener() {
//			
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				
//				System.out.println("click exit");
//				
//				share.exit();
//				
//				dispose();
//			}
//		};
//	}

}
